package com.taoyuan.common.utils.sign;

import com.taoyuan.common.constant.CharConstants;
import com.taoyuan.common.constant.NumberConstants;

/**
 * Base64工具类
 *
 * @author taoyuan
 */
public final class Base64 {
    static private final int BASELENGTH = 128;
    static private final int LOOKUPLENGTH = 64;
    static private final char PAD = '=';
    static final private byte[] BASE_64_ALPHABET = new byte[BASELENGTH];
    static final private char[] LOOK_UP_BASE_64_ALPHABET = new char[LOOKUPLENGTH];

    static {
        for (int i = 0; i < BASELENGTH; ++i) {
            BASE_64_ALPHABET[i] = -1;
        }
        for (int i = CharConstants.Z_CAPS; i >= CharConstants.A_CAPS; i--) {
            BASE_64_ALPHABET[i] = (byte) (i - CharConstants.A_CAPS);
        }
        for (int i = CharConstants.Z_UNCAPS; i >= CharConstants.A_UNCAPS; i--) {
            BASE_64_ALPHABET[i] = (byte) (i - CharConstants.A_UNCAPS + 26);
        }

        for (int i = CharConstants.NUM9; i >= CharConstants.NUM0; i--) {
            BASE_64_ALPHABET[i] = (byte) (i - CharConstants.NUM0 + 52);
        }

        BASE_64_ALPHABET['+'] = 62;
        BASE_64_ALPHABET['/'] = 63;

        for (int i = 0; i <= NumberConstants.NUM25; i++) {
            LOOK_UP_BASE_64_ALPHABET[i] = (char) (CharConstants.A_CAPS + i);
        }

        for (int i = NumberConstants.NUM26, j = 0; i <= NumberConstants.NUM51; i++, j++) {
            LOOK_UP_BASE_64_ALPHABET[i] = (char) (CharConstants.A_UNCAPS + j);
        }

        for (int i = NumberConstants.NUM52, j = 0; i <= NumberConstants.NUM61; i++, j++) {
            LOOK_UP_BASE_64_ALPHABET[i] = (char) (CharConstants.NUM0 + j);
        }
        LOOK_UP_BASE_64_ALPHABET[62] = (char) '+';
        LOOK_UP_BASE_64_ALPHABET[63] = (char) '/';
    }

}
